/*
 * 文件名：SHA1.java
 * 版权：Copyright by will_awoke
 * 描述：
 * 修改人：will_awoke
 * 修改时间：2014-6-23
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.qbgs.security;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.log4j.Logger;


/**
 * SHA1(Secure Hash Algorithm)摘要算法
 * @author will_awoke
 * @version 2014-6-23
 * @see SHA1
 * @since
 */
/**
 * 
 * @author will_awoke
 * @version 2014-6-23
 * @see SHA1
 * @since
 */
public class SHA1
{

    /**
     * 日志
     */
    private static final Logger LOG = Logger.getLogger(SHA1.class);

    /**
     * 
     * Description: 转换成SHA1密文
     * 
     * @param plainText 明文
     * @return String 进行过SHA1算法后的密文
     * @see
     */
    public static String toSHA1(String plainText)
    {
        try
        {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(plainText.getBytes());
            byte messageDigest[] = digest.digest();

            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            
            // 字节数组转换为 十六进制 数
            for (int i = 0; i < messageDigest.length; i++ )
            {

                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2)
                {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();

        }
        catch (NoSuchAlgorithmException e)
        {
            LOG.error("NoSuchAlgorithmException:", e);
        }
        return "";
    }

    /**  
     * for test
     * @param args 
     * @see 
     */
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub

    }

}
